unit Serv_Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
  rtcInfo, rtcConn, rtcTcpSrv;

type
  TDelphiService = class(TService)
    TcpServer_1: TRtcTcpServer;
    procedure ServiceContinue(Sender: TService; var Continued: Boolean);
    procedure ServiceExecute(Sender: TService);
    procedure ServicePause(Sender: TService; var Paused: Boolean);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceShutdown(Sender: TService);
    procedure TcpServer_1DataReceived(Sender: TRtcConnection);
  private
    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

var
  DelphiService: TDelphiService;

implementation

uses
  Form_Main,Class_KzDebug;

{$R *.DFM}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  DelphiService.Controller(CtrlCode);
end;

function TDelphiService.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TDelphiService.ServiceContinue(Sender: TService;
  var Continued: Boolean);
begin
  while not Terminated do
  begin
    Sleep(10);
    ServiceThread.ProcessRequests(False);
  end;
end;

procedure TDelphiService.ServiceExecute(Sender: TService);
begin
  while not Terminated do
  begin
    Sleep(10);
    ServiceThread.ProcessRequests(False);
  end;
end;

procedure TDelphiService.ServicePause(Sender: TService;
  var Paused: Boolean);
begin
  Paused:=True;
end;

procedure TDelphiService.ServiceStop(Sender: TService;
  var Stopped: Boolean);
begin
  Stopped := True;
  gbCanClose := True;
  TcpServer_1.StopListen;
  KzDebug.FileLog('server is stopping');    
  FormMain.Free;
end;

procedure TDelphiService.ServiceStart(Sender: TService;
  var Started: Boolean);
begin
  Started := True;
  Svcmgr.Application.CreateForm(TFormMain, FormMain);
  gbCanClose := False;

  TcpServer_1.ServerAddr:='192.168.0.51';
  TcpServer_1.ServerPort:='1861';
  TcpServer_1.Listen();
  KzDebug.FileLog('server is running');
end;

procedure TDelphiService.ServiceShutdown(Sender: TService);
begin
  gbCanClose := true;
  FormMain.Free;
  Status := csStopped;
  ReportStatus();
end;

procedure TDelphiService.TcpServer_1DataReceived(Sender: TRtcConnection);
var
  TextA:string;
begin
  TextA:=Sender.Read;

  Sender.Write(Format('received %s',[TextA]));
end;

end.
